<?php
namespace app\admin\service;

class Account 
{
    public function getToken($params)
    {
        $scope=$params['scope'];
        $uid=$params['id'];
        $values=[
            'scope'=>$scope,
            'uid'=>$uid
        ];
        $token=$this->saveToCache($values);
        return $token;
    }
    private function saveToCache($values)
    {
        $token=$this->generateToken();
        $expire_in=config('secure.token_expire_in');
        $result=cache($token,json_encode($values),$expire_in);
        if(!$result){
            throw new \Exception('服务器缓存异常');
        }
        return $token;
    }
   
    public static function generateToken()
    {
        $randChar=getRandChar(32);
        $timestamp=$_SERVER['REQUEST_TIME_FLOAT'];
        $tokenSalt=config('secure.token_salt');
        return md5($randChar.$timestamp.$tokenSalt);
    }
}